package ferhrosa.android;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewWrapper {
    private TextView text = null;
    private TextView subtext = null;
    private ImageView icon = null;
    private View row = null;
    private int templateId = 0;

    // Set the right template view to be visible
    private void setTemplate() {
        row.findViewById(R.id.Row01).setVisibility(View.GONE);
        row.findViewById(R.id.Row02).setVisibility(View.GONE);
        row.findViewById(R.id.Row03).setVisibility(View.GONE);
        switch (templateId) {
        case ListViewAdapter.TPL_1:
            row.findViewById(R.id.Row01).setVisibility(View.VISIBLE);
            break;
        case ListViewAdapter.TPL_2:
            row.findViewById(R.id.Row02).setVisibility(View.VISIBLE);
            break;
        case ListViewAdapter.TPL_3:
            row.findViewById(R.id.Row03).setVisibility(View.VISIBLE);
            break;
        }
    }

    public ListViewWrapper(View row) {
        this.row = row;
    }

    // Fill the template with the right values
    public void populateFrom(ListViewItem r) {
        getText().setText(r.getTitle());
        getSubtext().setText(r.getDescription());
        if (templateId == ListViewAdapter.TPL_1) {
//            getIcon().setImageResource(
//                    ImageUtilities.getDrawableById(r.getType()));
        }
        if (templateId == ListViewAdapter.TPL_2) {
//            getIcon().setImageResource(
//                    ImageUtilities.getDrawableById(r.getType()));
        }
        setTemplate();

    }

    public int getTemplateId() {
        return templateId;
    }

    public void setTemplateId(int templateId) {
        this.templateId = templateId;
        switch (templateId) {
        case ListViewAdapter.TPL_1:
            break;
        case ListViewAdapter.TPL_2:
            break;
        }
    }

    TextView getText() {
        if (text == null) {
            switch (templateId) {
            case ListViewAdapter.TPL_1:
                text = (TextView) row.findViewById(R.id.title);
                break;
            case ListViewAdapter.TPL_2:
                text = (TextView) row.findViewById(R.id.CommentText);
                break;
            case ListViewAdapter.TPL_3:
                text = (TextView) row.findViewById(R.id.ctitle);
                break;
            }
        }
        return (text);
    }

    // Alternative text (all except template 2)
    TextView getSubtext() {
        if (subtext == null) {
            switch (templateId) {
            case ListViewAdapter.TPL_1:
                subtext = (TextView) row.findViewById(R.id.description);
                break;
            case ListViewAdapter.TPL_3:
                subtext = (TextView) row.findViewById(R.id.cdescription);
                break;
            }
        }
        return (subtext);
    }

    ImageView getIcon() {
        if (icon == null) {
            switch (templateId) {
            case 1:
                icon = (ImageView) row.findViewById(R.id.icon);
                break;
            case 2:
                icon = (ImageView) row.findViewById(R.id.CommentRank);
                break;
            }

        }
        return (icon);
    }
}